package gamagora.game.entity
{
	import flash.display.BitmapData;
	
	import gamagora.game.utils.Utils;

	public class OSPillar extends OSEntity
	{
		private static const TILE_WIDTH:uint = 20;
		private static const TILE_HEIGHT:uint = 20;
		private static const ERODED_TILE_WIDTH:uint = 20;
		private static const ERODED_TILE_HEIGHT:uint = 4;
		
		protected var _widthInTiles:uint;
		protected var _heightInTiles:uint;
		
		public function OSPillar(X:Number, Y:Number, w:Number, h:Number)
		{
			super(NOTHING, X, Y);
			
			makeGraphic(w, h, 0xff000000, true);
			
			_widthInTiles = w / TILE_WIDTH;
			_heightInTiles = h / TILE_HEIGHT;
			
			erode();
		}
		
		private function erode():void
		{
			//Haut
			for(var i:uint=0;i<_widthInTiles;i++)
			{
				var erodedBlock:BitmapData = Utils.getRandomErodedBorder();
				var start:uint = i * TILE_WIDTH;
				for(var j:uint=start;j < start + ERODED_TILE_WIDTH;j++)
				{
					for(var k:uint=0;k<ERODED_TILE_HEIGHT;k++)
					{
						_pixels.lock();
						_pixels.setPixel32(j, k, erodedBlock.getPixel32(j - start, k));
						_pixels.unlock();
					}
				}
			}
			
			//Bas
			for(i=0;i<_widthInTiles;i++)
			{
				erodedBlock = Utils.getRandomErodedBorder();
				start = i * TILE_WIDTH;
				for(j=start;j < start + ERODED_TILE_WIDTH;j++)
				{
					for(k=0;k<ERODED_TILE_HEIGHT;k++)
					{
						_pixels.lock();
						_pixels.setPixel32(j, height  - k, erodedBlock.getPixel32(j - start, k));
						_pixels.unlock();
					}
				}
			}
			
			//Gauche
			for(i=0;i<_heightInTiles;i++)
			{
				erodedBlock = Utils.getRandomErodedBorder();
				start = i * TILE_HEIGHT;
				for(j=0;j < ERODED_TILE_HEIGHT;j++)
				{
					for(k=start;k<start + ERODED_TILE_WIDTH;k++)
					{
						_pixels.lock();
						_pixels.setPixel32(j, k, erodedBlock.getPixel32(k - start, j));
						_pixels.unlock();
					}
				}
			}
			
			//Droite
			for(i=0;i<_heightInTiles;i++)
			{
				erodedBlock = Utils.getRandomErodedBorder();
				start = i * TILE_HEIGHT;
				for(j=0;j < ERODED_TILE_HEIGHT;j++)
				{
					for(k=start;k<start + ERODED_TILE_WIDTH;k++)
					{
						_pixels.lock();
						_pixels.setPixel32(width - j, k, erodedBlock.getPixel32(k - start, j));
						_pixels.unlock();
					}
				}
			}
			
			resetHelpers();
		}
	}
}
